package com.huawei.chenxuan;

import lombok.extern.slf4j.Slf4j;
import com.huawei.chenxuan.Computer;
import com.huawei.chenxuan.Judge;
import com.huawei.chenxuan.JudgeIMPH;
import com.huawei.chenxuan.Person;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @program:IntelliJ IDEA
 * @Description:建立游戏的窗口类
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/5/10 16:36
 */
@Slf4j
public class Window extends JFrame implements Runnable{
    //常量
    private final String VOID_CHOOSE = "无";
    private final String STONE_CHOOSE = "石头";
    private final String SCICCORS_CHOOSE = "剪刀";
    private final String CLOTH_CHOOSE = "布";
    private final String PERSON = "人";
    private final String COMPUTER = "电脑";


    //创建窗口对象
    JFrame gameWindow = new JFrame("石头剪刀布");

    //创建下拉选项框
    String[] User = new String[]{"电脑","人"};
    JComboBox<String> comboBox1 = new JComboBox<>(User);
    JComboBox<String> comboBox2 = new JComboBox<>(User);

    //创建文本数据域，用于给人和电脑命名
    JTextArea name1 = new JTextArea(1,10);
    JTextArea id1 = new JTextArea(1,10);
    JTextArea name2 = new JTextArea(1,10);
    JTextArea id2 = new JTextArea(1,10);
    JLabel uresName1  =  new JLabel("姓名: ");
    JLabel uresId1  =  new JLabel("Id: ");
    JLabel uresName2  =  new JLabel("姓名: ");
    JLabel uresId2  =  new JLabel("Id: ");
    JButton ok = new JButton("确定");

    //创建单选按钮
    ButtonGroup oneselfChoose1 = new ButtonGroup();//创建按件组
    JRadioButton voidChoose1 = new JRadioButton("无",true);
    JRadioButton sciccorsChoose1 = new JRadioButton("剪刀",false);
    JRadioButton clothChoose1 = new JRadioButton("布",false);
    JRadioButton stoneChoose1 = new JRadioButton("石头",false);
    ButtonGroup  oneselfChoose2 = new ButtonGroup();
    JRadioButton voidChoose2 = new JRadioButton("无",true);
    JRadioButton sciccorsChoose2 = new JRadioButton("剪刀",false);
    JRadioButton clothChoose2 = new JRadioButton("布",false);
    JRadioButton stoneChoose2 = new JRadioButton("石头",false);


    //开始游戏按钮
    JButton beignButton = new JButton("开始游戏");

    /**
     *@Description: 对于人手势单选框的监听器事件的处理
     *@Param: String
     *@return: int
     *@Author: Mr.Lin
     *@Date: 2022/5/11 12:11
     */
    private int getGuesture(String setGuesture){
        int guesture = -1;
        switch (setGuesture){
            case "无":
                guesture = -1;
                break;
            case "石头":
                guesture = 0;
                break;
            case "剪刀":
                guesture = 1;
                break;
            case "布":
                guesture = 2;
                break;
        }
        return guesture;
    }



    /**
     *@Description: 对于开始游戏的按钮的监听器处理
     *@Param: int int
     *@return: String
     *@Author: Mr.Lin
     *@Date: 2022/5/11 12:23
     */


    /**
     *@Description: 用于进行游戏角色的初始化
     *@Param: [userName, userId]
     *@return:[java.lang.String, java.lang.String]
     *@Author: Mr.Lin
     *@Date: 2022/5/14 17:09
     */
    private void chooseClass(String userName,String userId){
        if (comboBox1.getActionCommand().equals("人")) {
            Person person = new Person(userName,userId);
        }
        else if (comboBox2.getActionCommand().equals("电脑")){
            Computer computer = new Computer(userName,userId);
        }
    }


    /**
     *@Description: 初始化桌面组件
     *@Param: void
     *@return: void
     *@Author: Mr.Lin
     *@Date: 2022/5/10 20:43
     */
    public void init(){
        //将选择按钮,初始化名字放在北方位
        JPanel actorChoose = new JPanel();
        actorChoose.add(comboBox1);
        actorChoose.add(uresName1);
        actorChoose.add(name1);
        actorChoose.add(uresId1);
        actorChoose.add(id1);
        actorChoose.add(comboBox2);
        actorChoose.add(uresName2);
        actorChoose.add(name2);
        actorChoose.add(uresId2);
        actorChoose.add(id2);
        actorChoose.add(ok);
        //为选择框的按钮进行动作赋予
        comboBox1.setActionCommand(PERSON);
        comboBox1.setActionCommand(COMPUTER);
        comboBox2.setActionCommand(PERSON);
        comboBox2.setActionCommand(COMPUTER);

        //对ok按钮进行一个动作监听

        gameWindow.add(actorChoose,BorderLayout.NORTH);



        //将选择手势动作放在居中位置
        JPanel gestureChoose1 = new JPanel();//创建组件
        JPanel gestureChoose2 = new JPanel();//创建组件
        //将按钮都装入一个按钮组中，然后通过手势组件进行框架的呈现
        oneselfChoose1.add(voidChoose1);
        oneselfChoose1.add(stoneChoose1);
        oneselfChoose1.add(clothChoose1);
        oneselfChoose1.add(sciccorsChoose1);

        oneselfChoose2.add(voidChoose2);
        oneselfChoose2.add(stoneChoose2);
        oneselfChoose2.add(clothChoose2);
        oneselfChoose2.add(sciccorsChoose2);

        gestureChoose1.add(voidChoose1);
        gestureChoose1.add(sciccorsChoose1);
        gestureChoose1.add(stoneChoose1);
        gestureChoose1.add(clothChoose1);
        gestureChoose2.add(voidChoose2);
        gestureChoose2.add(sciccorsChoose2);
        gestureChoose2.add(stoneChoose2);
        gestureChoose2.add(clothChoose2);

        //给每一个选项都绑定动作命令
        voidChoose1.setActionCommand(VOID_CHOOSE);
        stoneChoose1.setActionCommand(STONE_CHOOSE);
        clothChoose1.setActionCommand(CLOTH_CHOOSE);
        sciccorsChoose1.setActionCommand(SCICCORS_CHOOSE);
        voidChoose2.setActionCommand(VOID_CHOOSE);
        stoneChoose2.setActionCommand(STONE_CHOOSE);
        clothChoose2.setActionCommand(CLOTH_CHOOSE);
        sciccorsChoose2.setActionCommand(SCICCORS_CHOOSE);


        //将按钮放在左右两侧位置
        gameWindow.add(gestureChoose1,BorderLayout.EAST);
        gameWindow.add(gestureChoose2,BorderLayout.WEST);

        //组装底部增加开始游戏按钮
        JPanel beignPanel = new JPanel();
        beignPanel.add(beignButton);
        gameWindow.add(beignPanel,BorderLayout.SOUTH);
        //给开始游戏按钮添加相应的动作
        beignButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String setGuesture1 = oneselfChoose1.getSelection().getActionCommand();
                String setGuesture2 = oneselfChoose2.getSelection().getActionCommand();
                Judge judge = new JudgeIMPH();
                String result = null;
                if (getGuesture(setGuesture1) == -1 || getGuesture(setGuesture2) == -1){
                    result = "请检查你的单选框的选择是否正常";
                    JOptionPane.showMessageDialog(gameWindow,result);
                }
                else{
                    result = judge.judge(getGuesture(setGuesture1), getGuesture(setGuesture2));
                    JOptionPane.showMessageDialog(gameWindow,"这句结果为"+result);
                }
            }
        });


        //设置退出方式
        gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //居中摆放
        gameWindow.setLocationRelativeTo(null);

        //自适应大小
        gameWindow.pack();

        //视为可见
        gameWindow.setVisible(true);
    }

    @Override
    public void run() {
        init();
    }
}